import createAxios, { getUrl } from '/@/utils/axios'
import type { AxiosRequestConfig } from 'axios'
import { state as uploadExpandState, fileUpload as uploadExpand } from '/@/components/mixins/baUpload'
import { isExternal } from '/@/utils/common'
import axios from 'axios'

export const url = '/worker/kefu.HttpIndex/'

/**
 * 客服系统初始化请求（获取配置信息）
 */
export function initialize() {
    return createAxios({
        url: url + 'index',
    })
}

/**
 * 文件上传
 */
export function upload(fd: FormData, params: anyObj = {}, config: AxiosRequestConfig = {}): ApiPromise {
    if (uploadExpandState() == 'enable') {
        return uploadExpand(fd, params, config)
    }

    return createAxios({
        url: url + 'upload',
        method: 'POST',
        data: fd,
        params: params,
        timeout: 0,
        ...config,
    })
}

/**
 * 商品列表
 */
export function goods(url: string, params: anyObj = {}, axiosConfig: AxiosRequestConfig = {}) {
    if (!isExternal(url)) {
        return createAxios({
            url,
            method: 'GET',
            params,
        })
    } else {
        const Axios = axios.create({
            url,
            timeout: 1000 * 10,
            responseType: 'json',
            method: 'GET',
            params: params,
        })
        return Axios(axiosConfig)
    }
}

/**
 * 订单列表
 */
export function orders(url: string, params: anyObj = {}, axiosConfig: AxiosRequestConfig = {}) {
    if (!isExternal(url)) {
        return createAxios({
            url,
            method: 'GET',
            params,
        })
    } else {
        const Axios = axios.create({
            url,
            timeout: 1000 * 10,
            responseType: 'json',
            method: 'GET',
            params: params,
        })
        return Axios(axiosConfig)
    }
}

/**
 * 生成文件后缀icon的svg图片
 * @param suffix 后缀名
 * @param background 背景色,如:rgb(255,255,255)
 */
export function buildSuffixSvgUrl(suffix: string, background = '') {
    return getUrl() + url + 'buildSuffixSvg' + '?suffix=' + suffix + (background ? '&background=' + background : '') + '&server=1'
}

/**
 * 生成消息铃声的url
 */
export function buildRingUrl() {
    return getUrl() + url + 'ring?server=1'
}
